package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.excpetion.CustException;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 收藏行为业务层
 */
@Service("ApReadBehaviorService")
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    /**
     * 记录阅读行为
     *
     * @param dto 阅读行为DTO
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDTO dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null && dto.getEquipmentId() == null)
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);

        //用户使用的账号登录
        if (user != null) {
            ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
            //向阅读库查询记录
            ApReadBehavior apReadBehavior = mongoTemplate.findOne(Query.query(
                            Criteria.where("entryId").is(apBehaviorEntry.getId())),
                    ApReadBehavior.class);

            //已经阅读过 将阅读次数加1 否则创建阅读数据
            if (apReadBehavior != null) {
                apReadBehavior.setCount((short) (apReadBehavior.getCount() + 1));
                mongoTemplate.save(apReadBehavior);
            } else {
                //用户还未阅读过
                ApReadBehavior behavior = new ApReadBehavior();
                behavior.setArticleId(dto.getArticleId());
                behavior.setEntryId(String.valueOf(apBehaviorEntry.getId()));
                behavior.setCount((short) 1);
                behavior.setCreatedTime(new Date());
                behavior.setLoadDuration(dto.getLoadDuration());
                behavior.setPercentage(dto.getPercentage());
                behavior.setReadDuration(dto.getReadDuration());
                mongoTemplate.save(behavior);
            }

            // 向MQ发送文章阅读消息
            {
                UpdateArticleMess articleMess = new UpdateArticleMess();
                articleMess.setArticleId(dto.getArticleId());
                articleMess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
                articleMess.setAdd(1);
                rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(articleMess));
            }
        }

        return ResponseResult.okResult();
    }
}
